
/* GCSx
** FOLDER.H
**
** Folders within world browser (user-definable)
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_FOLDER_H_
#define __GCSx_FOLDER_H_

// Note that folders currently store no content- all data is
// stored as header data (hence no locking/caching needed) as
// there would never be a reason or opportunity to cache out
// anything relating to a folder during editing. If we later add
// "folder notes" or something, this might change.

class FolderEdit : virtual public SaveLoad {
protected:
    // Is the library cached to our file and not in memory?
    int cached;
    FileRead* cacheFile;
    int lockCount;
    
    // Name of folder, where it's located
    std::string name;
    class World* world;
    
    // Numeric ID- computer-generated- unique to world- nonzero
    int id;
    
    // Contents of folder
    struct FolderItem {
        // We track the item's type using already-defined file block types
        // We mostly only need type/id for saving and loading
        int itemBlockType;
        int itemId;
        
        // Most folder functionality just passes on to the items themselves
        // We do not own these pointers
        SaveLoad* item;
    };
    std::vector<FolderItem> items;
    
    // Currently sorted? (and add items sorted)
    int sorted;
    
    // Default folder for adding new items of given type?
    int defaultFont;
    int defaultTileSet;
    int defaultScene;
    int defaultAnimGroup;
    int defaultScript;
    int defaultLibrary;
    
    // Icon to use
    int icon;
    
    // Has the folder been modified from initial state (from empty if new)
    int headerModified;
    int contentModified;
    int disassociated;

    // Our node on the world browser
    TreeView* browserNode;

    // Set us as modified
    // Setting content modified also loads from cache if needed
    // Call these before actually making any modifications
    // (if header changes affect content, call both modifieds first)
    void setHeaderModified();
    void setContentModified();

public:
    // Id of 0 = we're going to load immediately
    // Any other id = we start out modified
    FolderEdit(class WorldEdit* myWorld, int myId = 0); // Starts with default settings
    virtual ~FolderEdit();
    
    // Accessors
    int getId() const { return id; }
    int getBlockType() const { return WorldFileLoad::BLOCKTYPE_FOLDER; }
    const std::string& getName() const { return name; }
    
    // Tells us to disassociate ourselves from anything, we've been "deleted"
    // or that we've been "readded" again
    // FileException means couldn't decache, and nothing was done
    void disassociate() throw_File;
    // Node is world browser node that we readd to
    void reassociate(TreeView* node);

    class WorldEdit* getWorldEdit() { return dynamic_cast<WorldEdit*>(world); }
    
    // Change properties; handles undo/update events
    void setName(const std::string& newName, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    //@TODO:void setSort(int nSorted) throw_Undo;
    //@TODO:void setIcon(int nIcon) throw_Undo;
    //@TODO:void setDefaults(int nFont, int nTileSet, int nScene, int nAnimGroup, int nScript, int nLibrary) throw_Undo;

    // Handles undo/update events; updates world browser node appropriately,
    // including expanding/cursor focus (presumes focus on newly added items)
    void addItem(SaveLoad* item, int index = -1, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    void removeItem(SaveLoad* item, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    //@TODO:void moveItem(SaveLoad* item, int pos, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;

    // Returns true if OK pressed
    int propertiesDialog(Window* srcWin = NULL, Window* exWin = NULL);

    // Tells folder to add itself to the world browser node
    void addToBrowser(TreeView* node, int makeCurrent = 1);
    void dropBrowser(); // Called if animgroup gets dropped from browser

    // Treeview event handling
    int treeviewEvent(int code, int command, int check);
    static int treeviewEventWrap(void* ptr, int code, int command, int check);

    // File access
    void loadHeader(FileRead* file) throw_File;
    void loadContent(FileRead* file);
    int isContentCached() const;
    void cacheLoad() throw_File;
    int markLock() throw_File;
    int markUnlock();
    int isLocked() const;
    int isHeaderModified() const;
    int isContentModified() const;
    Uint32 saveHeader(FileWrite* file) throw_File;
    void saveContent(FileWrite* file) throw_File;
    void saveSuccess();
    void cachedContent(FileRead* file, int oldData);
};

#endif

